package com.open.demo.compose

import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.focusable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.scale
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.focus.onFocusChanged
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.open.demo.R

object ItemWidget {

    @Preview
    @Composable
    fun ItemView(
        item:Item,
        isFocused:Boolean,
        modifier: Modifier = Modifier) {
        val scale by animateFloatAsState(targetValue = if (isFocused) 1.1f else 1f)
        Box(
            modifier=modifier.scale(scale = scale)
                .shadow(if (isFocused) 20.dp else 0.dp)
                .border(2.dp, if (isFocused) MaterialTheme.colors.surface else Color.Transparent)
                .focusable()) {
            Image(
                painter = painterResource(item.res),
                contentScale = ContentScale.Crop,
                contentDescription = null,
                modifier = Modifier
                    .fillMaxSize()
            )
            Text(
                text = item.title,
                color = Color.White,
                textAlign = TextAlign.Center,
                style = MaterialTheme.typography.caption,
                modifier = Modifier
                    .background(Color.Gray.copy(alpha = 0.5f))
                    .padding(5.dp)
                    .fillMaxWidth()
                    .align(Alignment.BottomCenter)
            )
        }
    }


}